$(function () {

    console.log('js文件部署成功');


    var $editInfo = $('#editInformationRequired'),
        $confirm_edit_btn = $('#confirm-edit-btn'),
        $text_inputs = $("tr input[type='text'],select");

    /*input默认为不可选中*/
    $text_inputs.css('outline','none');


    function valueToInputText() {
        var $cardType = $('#cardType'),
            $creditCard = $('#creditCard'),
            $expiryDate = $('#expiryDate'),
            $firstName = $('#firstName'),
            $lastName = $('#lastName'),
            $address1 = $('#address1'),
            $address2 = $('#address2'),
            $city = $('#city'),
            $state = $('#state'),
            $zip = $('#zip'),
            $country = $('#country');
        //将后台的值重新赋值给text
        // $cardType.val($cardType.attr('value'));
        $creditCard.val($creditCard.attr('value'));
        $expiryDate.val($expiryDate.attr('value'));
        $firstName.val($firstName.attr('value'));
        $lastName.val($lastName.attr('value'));
        $address1.val($address1.attr('value'));
        $address2.val($address2.attr('value'));
        $city.val($city.attr('value'));
        $state.val($state.attr('value'));
        //测试值China
        console.log($state.attr('value'));
        $zip.attr('value',$zip.val());
        $country.attr('value',$country.val());

    }
    function ableEditStatus() {
        //将表格中所有input设置成可编辑
        $('select').removeClass('select-readonly');
        $text_inputs.removeAttr('readonly');
        // input设置为可选中
        $text_inputs.css('outline',' -webkit-focus-ring-color auto 5px');
        //出现确认编辑按钮
        $confirm_edit_btn.show();
    }
    function disableEditStatus() {
        //将表格中所有input设置成不可编辑
        $('select').addClass('select-readonly');
        $text_inputs.removeAttr('readonly');
        /*input默认为不可选中*/
        $text_inputs.css('outline','none');
        //隐藏确认编辑按钮
        $confirm_edit_btn.hide();
    }



    $editInfo.on('click',function () {
        if($editInfo.is(':checked')){
            ableEditStatus();
        }else{
            valueToInputText();
            disableEditStatus();
        }
    });

    //点击确认编辑按钮
    $confirm_edit_btn.on('click',function (event) {
        //获取表格中所有值
        var $cardType = $('#cardType'),
            $creditCard = $('#creditCard'),
            $expiryDate = $('#expiryDate'),
            $firstName = $('#firstName'),
            $lastName = $('#lastName'),
            $address1 = $('#address1'),
            $address2 = $('#address2'),
            $city = $('#city'),
            $state = $('#state'),
            $zip = $('#zip'),
            $country = $('#country');
        //将值传给后台，生成新数据的订单
        $.ajax({
            type:'GET',
            url:'editOrderInfo',
            data:{'cardType':$cardType.val(),'creditCard':$creditCard.val(),
                'expiryDate':$expiryDate.val(),'firstName':$firstName.val(),
                'lastName':$lastName.val(),'address1':$address1.val(),
                'address2':$address2.val(),'city':$city.val(),
                'state':$state.val()},
            success:function (data) {
                // confirm('订单编辑成功');
                // event.preventDefault();
            },
            error:function (errorMsg){
                console.log(errorMsg);
            }
        });



        $cardType.attr('value',$cardType.val());
        $creditCard.attr('value',$creditCard.val());
        $expiryDate.attr('value',$expiryDate.val());
        $firstName.attr('value',$firstName.val());
        $lastName.attr('value',$lastName.val());
        $address1.attr('value',$address1.val());
        $address2.attr('value',$address2.val());
        $city.attr('value',$city.val());
        $state.attr('value',$state.val());
        //测试值China
        console.log($state.attr('value'));
        $zip.attr('value',$zip.val());
        $country.attr('value',$country.val());


        disableEditStatus();
        //将CheckBox设为未选中状态
        $editInfo.prop('checked',false);
        //提示编辑成功
        var result = confirm('信息编辑成功！');
        if(result){
            event.preventDefault();
        }else{
            valueToInputText();
        }

    })


})